package com.pactera.madp.cp.controller.oms.medium;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.common.security.annotation.Inner;
import com.pactera.madp.cp.common.util.file.FilesUtil;
import com.pactera.madp.cp.handler.CorpContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @description:上传
 * @author: Huayu
 * @time: 2020/11/22 14:30
 */
@RestController
@AllArgsConstructor
@RequestMapping("/common")
@Api(value = "common", tags = "上传")
public class UploadController {

    @SysLog("文件上传-临时素材")
    @PostMapping("/upload")
    @ApiOperation(value = "文件上传")
    public R uploadBlog(@RequestParam("file") MultipartFile file){
        String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
        return  R.ok(FilesUtil.uploadFile(file,"/medium/corp-"+ CorpContextHolder.getCorpId() +"/"+fileName,true));
    }

    @SysLog("文件上传-推送消息文件")
    @PostMapping("/uploads")
    @ApiOperation(value = "推送消息文件上传")
    public R uploadFile(@RequestParam("file") MultipartFile file){
        String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
        return  R.ok(FilesUtil.uploadFile(file,"/files/corp-"+ CorpContextHolder.getCorpId() +"/"+fileName,true));
    }

    @Inner(value = false)
    @PostMapping("/upload-test")
    @ApiOperation(value = "文件上传测试用")
    public R uploadTest(@RequestParam("file") MultipartFile file){
        String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
        return  R.ok(FilesUtil.uploadFile(file,"/testFile" +"/"+fileName,true));
    }

}
